package studio.raptor.hub.in.config;

import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Created by liuyong8 on 2017/9/21.
 */
@Configuration
public class ExecutorConfig {
  /** Set the ThreadPoolExecutor's core pool size. */
  public static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();

  /** Set the ThreadPoolExecutor's maximum pool size. */
  public static final int MAX_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 40;

  /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
  private static final int QUEUE_CAPACITY = Runtime.getRuntime().availableProcessors();

  @Bean
  public Executor executor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(CORE_POOL_SIZE);
    executor.setMaxPoolSize(MAX_POOL_SIZE);
    executor.setQueueCapacity(QUEUE_CAPACITY);
    executor.setThreadNamePrefix("node_msg_");
    executor.initialize();
    return executor;
  }
}
